<template>
  <el-dialog
    class="pms-dialog"
    title="配置明细"
    :visible.sync="dialogVisible"
    width="70%"
    @open="handleQuery"
    @closed="handleClose"
  >
    <div class="tips">
      【说明】：PMS“乙供物资明细配置”功能是项目经理针对项目中使用的总包服务（施工总承包、EPC总承包）、外电引入施工服务、砖混/框架机房施工服务确定服务内主设备物资的数量与金额，
      以便资源与财务系统明确该服务具体做了哪些工作，是否需要进行装配、转资，如何装配、转资及形成什么类型的资源、资产主设备信息。
    </div>
    <el-tabs v-model="activeName" type="border-card" @tab-click="getList">
      <el-tab-pane v-for="item in tabsList" :key="item.name" :label="item.label" :name="item.name">
        <el-table ref="table" :data="tableData" border>
          <el-table-column align="center" prop="siteId" label="站址编码" width="200" />
          <el-table-column align="center" prop="pmsServiceCode" label="物料编码" width="200" />
          <!-- 新需求添加是否组合件 -->
          <el-table-column align="center" label="是否组合件" width="100">
            <template slot-scope="scope">
              {{ scope.row.ifMultiName }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="pmsServiceName" label="物料名称" />
          <el-table-column align="center" prop="sumMoney" width="150" label="累计验工计价金额" />
          <el-table-column align="center" prop="sumProportion" width="150" label="明细占比总和(%)" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <el-button
                v-if="urlParams.ifEdit === '1'"
                class="pms-button-primary operate-btn"
                @click="handleConfiguration(scope.row, true)"
              >
                配置明细
              </el-button>
              <el-button class="pms-button-primary operate-btn" @click="handleConfiguration(scope.row, false)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <ServiceConfigDetails ref="details" @refreshTable="refreshTable" />
    </el-tabs>
    <div slot="footer" class="foot-btn">
      <el-button
        v-if="urlParams.ifEdit !== '0'"
        type="primary"
        class="pms-button-primary"
        @click="handleSend"
      >确认</el-button>
      <!-- <el-button type="primary" @click="dialogVisible = false" class="pms-button-primary">关闭</el-button> -->
    </div>
  </el-dialog>
  <!-- <div style="padding:20px">
    <div class="tips">
      【说明】：PMS“乙供物资明细配置”功能是项目经理针对项目中使用的总包服务（施工总承包、EPC总承包）、外电引入施工服务、砖混/框架机房施工服务确定服务内主设备物资的数量与金额， 以便资源与财务系统明确该服务具体做了哪些工作，是否需要进行装配、转资，如何装配、转资及形成什么类型的资源、资产主设备信息。
    </div>
    <el-tabs v-model="activeName" type="border-card" @tab-click="getList">
      <el-tab-pane v-for="item in tabsList" :key="item.name" :label="item.label" :name="item.name">
        <el-table ref="table" :data="tableData" border>
          <el-table-column align="center" prop="siteId" label="站址编码" width="200" />
          <el-table-column align="center" prop="pmsServiceCode" label="物料编码" width="200" />
          <el-table-column align="center" prop="pmsServiceName" label="物料名称" />
          <el-table-column align="center" prop="sumMoney" width="150" label="累计验工计价金额" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <el-button v-if="urlParams.ifEdit === '1'" class="pms-button-primary operate-btn" @click="handleConfiguration(scope.row,true)">
                配置明细
              </el-button>
              <el-button class="pms-button-primary operate-btn" @click="handleConfiguration(scope.row,false)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <ServiceConfigDetails ref="details" />
    </el-tabs>
    <div style="display:flex;justify-content: center;margin-top:10px">
      <el-button v-if="urlParams.ifEdit !== '0'" type="primary" class="pms-button-primary" @click="handleSend">确认</el-button>
    </div>
  </div>-->
</template>

<script>
import ServiceConfigDetails from './ServiceConfigDetails'
import {
  isPilotProject,
  initServiceDetailData,
  isWhitePrjApi,
  getListNew,
  getSumMoneyIsNull,
  deleteAllUselessDetail4,
  deleteAllUselessDetail,
  saveDetailVerifyRevocation,
  saveDetailVerify
} from '@/api/acceptanceManagement'
import { parseParams } from '@/utils'

export default {
  name: 'ServiceTabs',
  components: {
    ServiceConfigDetails
  },
  data() {
    return {
      activeName: '1',
      dialogVisible: false,
      tableData: [],
      tabsList: [
        { label: '总包服务', name: '1' },
        { label: '外电引入施工服务', name: '2' },
        { label: '砖混/框架机房服务', name: '3' }
      ],
      urlParams: {
        prjId: '',
        subPrjId: null,
        ifEdit: '1',
        type: null,
        isWhite: false,
        ifSingleProject: ''
      },
      row: {}
    }
  },
  created() {
    // this.urlParams.type : 4 （销项）
    this.urlParams = {
      ...this.urlParams,
      ...parseParams(window.location.hash)
    }
  },
  methods: {
    refreshTable(val) {
        this.handleQuery(val)
    },
    shoService(data) {
      this.urlParams.prjId = data.prjId
      this.urlParams.subPrjId = data.subPrjId
      this.urlParams.ifSingleProject = data.ifSingleProject
      this.urlParams.type = data.type
      this.urlParams.ifEdit = data.ifEdit
      this.dialogVisible = true
    },
    async handleQuery(val = '1') {
      const obj = {
        prjId: this.urlParams.prjId // this.urlParams.ifSingleProject === '0' ? this.urlParams.prjId : this.urlParams.subPrjId
      }
      const { data } = await isPilotProject(obj)
      this.urlParams.isPilot = data
      const dataObj = {
        prjId: this.urlParams.prjId,
        subPrjId: this.urlParams.ifSingleProject === '0' ? null : this.urlParams.subPrjId,
        isPilot: data
      }
      this.urlParams.type !== '4' && initServiceDetailData(dataObj)

      const isWhite = await isWhitePrjApi(obj)
      // 白名单
      if (+isWhite.data !== 0 && data !== 0) {
        // this.urlParams.isPilot = 0
        this.urlParams.isWhite = true
      } else {
        this.urlParams.isWhite = false
      }
      this.$nextTick(() => {
        this.getList({ name: val })
      })
    },
    // 配置明细 or 查看
    handleConfiguration(row, editable) {
      if (+this.urlParams.type !== 4 && row.ifHave === '1' && editable) {
        return this.$message.error('此站址服务下存在未推送或项目进度不为100%的验工计价')
      }
      if (+row.ifHave === 0 && +row.sumMoney === 0 && editable) {
        return this.$message.error(`此站址【${row.siteId}】下的服务【${row.pmsServiceCode}】验工计价已全部损失,无需配置明细`)
      }
      this.$refs['details'].getRow({ ...row, type: this.activeName, editable, urlParams: this.urlParams, typeIndex: this.activeName })
    },
    getList(val) {
      const obj = {
        prjId: this.urlParams.prjId,
        subPrjId: this.urlParams.subPrjId,
        type: val.name
      }
      getListNew(obj).then(res => {
        this.tableData = res.data
      })
      this.$nextTick(() => {
        this.$refs['table'][val.name - 1].doLayout()
      })
    },
   async handleSendOne(obj){
     let res = null
     if (this.urlParams.type === '4') {
       res = await deleteAllUselessDetail4({ ...obj, isPilot: 0 })
     } else {
       res = await deleteAllUselessDetail({ ...obj, isPilot: this.urlParams.isWhite ? 0 : this.urlParams.isPilot })
     }
     if (res.code !== '200') {
       return Promise.reject()
     }
   },
   async handleSendTwo(obj){
     let response = null
     console.log(this.urlParams, 123123)
     if (+this.urlParams.type === 4) {
       response = await saveDetailVerifyRevocation(obj)
     } else {
       response = await saveDetailVerify({ ...obj, isPilot: this.urlParams.isWhite ? 0 : this.urlParams.isPilot })
     }
     if (response.code !== '200') {
       return Promise.reject()
     }
     this.$message.success(response.message)
     this.dialogVisible = false
   },
    // 确认
    async handleSend() {
      const obj = {
        prjId: this.urlParams.prjId,
        subPrjId: this.urlParams.ifSingleProject === '0' ? null : this.urlParams.subPrjId
      }
      const h = this.$createElement
      const { data } = await getSumMoneyIsNull(obj)

      if (data > 0) {
        this.$confirm('提示', {
          title: '提醒',
          message: h('div', [h('p', '项目下存在累计验工计价为0但已配置了明细的服务，请确认是否删除明细？确认后在PC端添加的服务明细将自动删除？'), h('p', '（使用APP贴签赋码添加的明细请在APP端操作删除）。如不删除明细需对该服务做验工计价！')]),
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'pms-zr-button',
          confirmButtonClass: 'pms-zr-button-primary',
          type: 'warning'
        }).then(async() => {
          await this.handleSendOne(obj)
          this.$message.success('删除成功')
        })
        return
      }
      await this.handleSendTwo(obj)
    },
    handleClose() {
      this.activeName = '1'
      this.dialogVisible = false
    }
  }

}
</script>
<style lang="scss" scoped>
.tips {
  margin-bottom: 10px;
  color: #EB4B4B;
  line-height: 25px;
}
</style>
